package cn.weekimwee.firstlineofcodeforkotlin.unit7

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.net.Uri
import android.util.Log
import cn.weekimwee.firstlineofcodeforkotlin.unit6.MyDatabaseHelper

class FirstProvider : ContentProvider() {

    val BOOK_DIR = 0
    val BOOK_ITEM = 1
    val CATEGORY_DIR = 2
    val CATEGORY_ITEM = 3
    val AUTHORITY = "cn.weekimwee.firstlineofcodeforkotlin.provider"
    private val uriMather: UriMatcher = UriMatcher(UriMatcher.NO_MATCH)
            .apply {
                addURI(AUTHORITY, "book", BOOK_DIR)
                addURI(AUTHORITY, "book/#", BOOK_ITEM)
                addURI(AUTHORITY, "category", CATEGORY_DIR)
                addURI(AUTHORITY, "category/#", CATEGORY_ITEM)
            }
    lateinit var helper: MyDatabaseHelper

    /*init {
        uriMather.addURI(AUTHORITY, "book", BOOK_DIR)
        uriMather.addURI(AUTHORITY, "book/#", BOOK_ITEM)
        uriMather.addURI(AUTHORITY, "category", CATEGORY_DIR)
        uriMather.addURI(AUTHORITY, "category/#", CATEGORY_ITEM)
    }*/

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        val db = helper.writableDatabase
        var deletedRows = 0
        when (uriMather.match(uri)) {
            BOOK_DIR -> {
                deletedRows = db.delete("Book", selection, selectionArgs)
            }
            BOOK_ITEM -> {
                val bookId = uri.pathSegments[1]
                deletedRows = db.delete("Book", "id = ?", arrayOf(bookId))
            }
            CATEGORY_DIR -> {
                deletedRows = db.delete("Category", selection, selectionArgs)
            }
            CATEGORY_ITEM -> {
                val categoryId = uri.pathSegments[1]
                deletedRows = db.delete("Category", "id = ?", arrayOf(categoryId))
            }
        }
        return deletedRows
    }

    override fun getType(uri: Uri): String? {
        return when (uriMather.match(uri)) {
            BOOK_DIR -> "vnd.android.cursor.dir/vnd.cn.weekimwee.firstlineofcodeforkotlin.provider.book"
            BOOK_ITEM -> "vnd.android.cursor.item/vnd.cn.weekimwee.firstlineofcodeforkotlin.provider.book"
            CATEGORY_DIR -> "vnd.android.cursor.dir/vnd.cn.weekimwee.firstlineofcodeforkotlin.provider.category"
            CATEGORY_ITEM -> "vnd.android.cursor.dir/vnd.cn.weekimwee.firstlineofcodeforkotlin.provider.category"
            else -> null
        }
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        val db = helper.writableDatabase
        var uriReturn: Uri? = null
        when (uriMather.match(uri)) {
            BOOK_DIR, BOOK_ITEM -> {
                val newBookId = db.insert("Book", null, values)
                uriReturn = Uri.parse("content://$AUTHORITY/book/$newBookId")
            }
            CATEGORY_DIR, CATEGORY_ITEM -> {
                val newCategoryId = db.insert("Category", null, values)
                uriReturn = Uri.parse("content://$AUTHORITY/category/$newCategoryId")
            }
        }
        return uriReturn
    }

    override fun onCreate(): Boolean {
        Log.e("eeeee","onCreate")
        helper = MyDatabaseHelper(context, "BookStore.db", null, 2)
        Log.e("eeeee","onCreate--1")
        return true
    }

    override fun query(uri: Uri, projection: Array<String>?, selection: String?,
                       selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
        Log.e("eeeee","query----->")
        val db = helper.readableDatabase
        var cursor: Cursor? = null
        when (uriMather.match(uri)) {
            BOOK_DIR -> {
                cursor = db.query("Book", projection, selection, selectionArgs, null, null, sortOrder)
            }
            BOOK_ITEM -> {
                val bookId = uri.pathSegments[1]
                cursor = db.query("Book", projection, "id = ?", arrayOf(bookId), null, null, sortOrder)
            }
            CATEGORY_DIR -> {
                cursor = db.query("Category", projection, selection, selectionArgs, null, null, sortOrder)
            }
            CATEGORY_ITEM -> {
                val categoryId = uri.pathSegments[1]
                cursor = db.query("Category", projection, "id = ?", arrayOf(categoryId), null, null, sortOrder)
            }
        }
        return cursor
    }

    override fun update(uri: Uri, values: ContentValues?, selection: String?,
                        selectionArgs: Array<String>?): Int {
        val db = helper.writableDatabase
        var updatedRows = 0
        when (uriMather.match(uri)) {
            BOOK_DIR -> {
                updatedRows = db.update("Book", values, selection, selectionArgs)
            }
            BOOK_ITEM -> {
                val bookId = uri.pathSegments[1]
                updatedRows = db.update("Book", values, "id = ?", arrayOf(bookId))
            }
            CATEGORY_DIR -> {
                updatedRows = db.update("Category", values, selection, selectionArgs)
            }
            CATEGORY_ITEM -> {
                val categoryId = uri.pathSegments[1]
                updatedRows = db.update("Category", values, "id = ?", arrayOf(categoryId))
            }
        }
        return updatedRows
    }
}
